#include "rendercompwidget.h"

RenderCompWidget::RenderCompWidget(AED_RenderComp *comp, ImageManager *manager, ImageModel *img_model, QWidget *parent) :
    QWidget(parent),m_manager(manager),m_imgmodel(img_model)
{

    this->setMaximumHeight(200);
    QVBoxLayout *layout = new QVBoxLayout;
    this->setLayout(layout);

    group = new QGroupBox("Enable");
    layout->addWidget(group);
    group->setCheckable(true);

    QVBoxLayout *main_layout = new QVBoxLayout; // La layout principal
    group->setLayout(main_layout);


    QHBoxLayout *hbox1,*hbox2,*hbox3;
    hbox1 = new QHBoxLayout;
    hbox2 = new QHBoxLayout;
    hbox3 = new QHBoxLayout;


    QLabel *imagelabel = new QLabel("Image");
    QLabel *alphalabel = new QLabel("Alpha");
    QLabel *layerlabel = new QLabel("Layer");

    m_imagebox = new QComboBox();
    load_image_button = new QPushButton("Load Image");
    visiblebox = new QCheckBox("Visible");
    textbox = new QCheckBox("Text");
    alphabox = new QDoubleSpinBox;
    layerbox = new QSpinBox;

    main_layout->addLayout(hbox1);
    m_imagebox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    hbox1->addWidget(imagelabel);
    hbox1->addWidget(m_imagebox);
    //hbox1->addWidget(load_image_button);
    main_layout->addWidget(visiblebox);
    main_layout->addWidget(textbox);
    main_layout->addLayout(hbox2);
    hbox2->addWidget(alphalabel);
    hbox2->addWidget(alphabox);
    main_layout->addLayout(hbox3);
    hbox2->addWidget(layerlabel);
    hbox2->addWidget(layerbox);




    alphabox->setMaximum(1.0);
    alphabox->setMinimum(0.0);
    alphabox->setSingleStep(0.05);
    layerbox->setRange(0,9);



    connect(group, SIGNAL(toggled(bool)),
                this,SLOT(SetEnabled(bool)));

    connect(m_imagebox, SIGNAL(currentIndexChanged(QString)),
                this, SLOT(SetImage(QString)));
    connect(load_image_button, SIGNAL(pressed()),
                this, SLOT(LoadImageK()));
    connect(visiblebox, SIGNAL(stateChanged(int)),
                this, SLOT(SetVisible(int)));
    connect(textbox, SIGNAL(stateChanged(int)),
                this, SLOT(SetText(int)));
    connect(alphabox, SIGNAL(valueChanged(double)),
                this, SLOT(SetAlpha(double)));
    connect(layerbox, SIGNAL(valueChanged(int)),
                this, SLOT(SetLayer(int)));

    setComp(comp);

}

void RenderCompWidget::setComp(AED_RenderComp *comp){
    m_comp = comp;
    if(!m_comp){
        group->setEnabled(false);
    }else{
        group->setEnabled(true);
        group->setChecked(comp->IsEnabled());

        UpdateImageList();

        visiblebox->setChecked(comp->GetVisible());
        textbox->setChecked(comp->GetText());
        alphabox->setValue(comp->GetAlpha());
        layerbox->setValue(comp->GetLayer());
    }

}

void RenderCompWidget::SetImage(const QString &image){

    m_comp->SetImage(m_manager->GetImage(image));

}

void RenderCompWidget::SetVisible(int state){
    m_comp->SetVisible(state);
}

void RenderCompWidget::SetText(int state){
    m_comp->SetText(state);
}

void RenderCompWidget::SetLayer(int layer){
    m_comp->SetLayer(layer);
}

void RenderCompWidget::SetAlpha(double alpha){
    m_comp->SetAlpha(alpha);
}

void RenderCompWidget::SetEnabled(bool enabled){
    m_comp->SetEnabled(enabled);
}

void RenderCompWidget::UpdateImageList(){
    m_imagebox->blockSignals(true);
    m_imagebox->clear();
    m_imagebox->addItems(m_manager->GetImagePaths());
    m_imagebox->addItem("NONE");

    if(m_comp->GetImage()){

        m_imagebox->setCurrentIndex(m_imagebox->findText(m_comp->GetImage()->GetPath()));
    }else{
        m_imagebox->setCurrentIndex(m_imagebox->findText("NONE"));
    }
    m_imagebox->blockSignals(false);
}

void RenderCompWidget::LoadImageK(){
    QString fileName = QFileDialog::getOpenFileName(this,
         tr("Open Image"), "/home/", tr("Targa Files (*.tga)"));

    if(!fileName.isNull()){
        if(!m_imgmodel->LoadImageK(fileName)){
            QErrorMessage *error = new QErrorMessage(this);
            error->showMessage("Couldn't open the image");
        }
    }
    UpdateImageList();
}
